{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://f000.backblazeb2.com/file/malay-dataset/segmentation/test-set-segmentation.json\n",
    "# !wget https://f000.backblazeb2.com/file/malay-dataset/true-case/test-set-true-case.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['segmentation-iium-single.json',\n",
       " 'segmentation-singlish-short.json',\n",
       " 'segmentation-news-short.json',\n",
       " 'segmentation-sg-news-single.json',\n",
       " 'segmentation-en-ms-short.json',\n",
       " 'segmentation-iium.json',\n",
       " 'segmentation-iium-short.json',\n",
       " 'segmentation-news.json',\n",
       " 'segmentation-en-ms-single.json',\n",
       " 'segmentation-news-single.json',\n",
       " 'segmentation-wiki-short.json',\n",
       " 'segmentation-en-ms.json',\n",
       " 'segmentation-singlish-single.json',\n",
       " 'segmentation-sg-news-short.json',\n",
       " 'segmentation-wiki-single.json',\n",
       " 'segmentation-wiki.json']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "from glob import glob\n",
    "\n",
    "files = glob('segmentation-*.json')\n",
    "files = [f for f in files if 'test' not in f]\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "segmentation-iium-single.json\n",
      "segmentation-singlish-short.json\n",
      "segmentation-news-short.json\n",
      "segmentation-sg-news-single.json\n",
      "segmentation-en-ms-short.json\n",
      "segmentation-iium.json\n",
      "segmentation-iium-short.json\n",
      "segmentation-news.json\n",
      "segmentation-en-ms-single.json\n",
      "segmentation-news-single.json\n",
      "segmentation-wiki-short.json\n",
      "segmentation-en-ms.json\n",
      "segmentation-singlish-single.json\n",
      "segmentation-sg-news-short.json\n",
      "segmentation-wiki-single.json\n",
      "segmentation-wiki.json\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "with open('train.json', 'w') as fopen_jsonl:\n",
    "    for f in files:\n",
    "        print(f)\n",
    "        with open(f) as fopen:\n",
    "            data = json.load(fopen)\n",
    "            for l in data:\n",
    "                if len(l[0]) and len(l[1]):\n",
    "                    d = {\"translation\": {\"src\": l[0], \"tgt\": l[1], 'prefix': 'segmentasi: '}}\n",
    "                    fopen_jsonl.write(f'{json.dumps(d)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "files = glob('results-*.json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "results-wiki.json\n",
      "results-wiki-single.json\n",
      "results-news.json\n",
      "results-news-single.json\n",
      "results-sg-news-single.json\n"
     ]
    }
   ],
   "source": [
    "with open('train.json', 'a') as fopen_jsonl:\n",
    "    for f in files:\n",
    "        print(f)\n",
    "        with open(f) as fopen:\n",
    "            data = json.load(fopen)\n",
    "            for l in data:\n",
    "                if len(l[0]) and len(l[1]):\n",
    "                    d = {\"translation\": {\"src\": l[0], \"tgt\": l[1], 'prefix': 'kes benar: '}}\n",
    "                    fopen_jsonl.write(f'{json.dumps(d)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"translation\": {\"src\": \"Untuk pengetahuan semua, alhamdulillah aku dibesarkan dalam keluarga yang serba sederhana.\", \"tgt\": \"Untuk pengetahuan semua, alhamdulillah aku dibesarkan dalam keluarga yang serba sederhana.\", \"prefix\": \"segmentasi: \"}}\r\n",
      "{\"translation\": {\"src\": \"untuk pengetahuan semua, alhamdulillah aku dibesarkan dalam keluarga yang serba sederhana.\", \"tgt\": \"untuk pengetahuan semua, alhamdulillah aku dibesarkan dalam keluarga yang serba sederhana.\", \"prefix\": \"segmentasi: \"}}\r\n"
     ]
    }
   ],
   "source": [
    "!head -n 2 train.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "!shuf train.json > shuffled-train.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('test.json', 'w') as fopen_jsonl:\n",
    "    with open('test-set-segmentation.json') as fopen:\n",
    "        data = json.load(fopen)\n",
    "        for l in data:\n",
    "            if len(l[0]) and len(l[1]):\n",
    "                d = {\"translation\": {\"src\": l[0], \"tgt\": l[1], 'prefix': 'segmentasi: '}}\n",
    "                fopen_jsonl.write(f'{json.dumps(d)}\\n')\n",
    "                \n",
    "    with open('test-set-true-case.json') as fopen:\n",
    "        data = json.load(fopen)\n",
    "        for l in data:\n",
    "            if len(l[0]) and len(l[1]):\n",
    "                d = {\"translation\": {\"src\": l[0], \"tgt\": l[1], 'prefix': 'kes benar: '}}\n",
    "                fopen_jsonl.write(f'{json.dumps(d)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "!shuf test.json > shuffled-test.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "!head -n 4000 shuffled-test.json > test-4k.json"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
